package com.woniuxy.project115.kaer.tenant.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.Map;

/**
 * @author HuH
 * @version 1.0
 * @CreatTime 2025-02-25 14:58
 * @description xxljob工具类
 */
public class XxlJobUtil {

    /**
     *  动态创建一个定时任务
     * @param jobDesc 任务描述
     * @param executorHandler 执行器，任务Handler名称
     * @param executorParam 执行器，任务参数
     * @param executeTime 任务执行时间
     */
    public static void addXxlJob(String jobDesc, String executorHandler, Object executorParam, LocalDateTime executeTime) throws Exception {
//        String jobDesc = "";//任务描述
//        Integer seconds = 24 * 60 * 60;//延期时间：当前时间+seconds
//        String executorHandler = ""; //执行器，任务Handler名称
//        String executorParam = "";// 执行器，任务参数
        String xxlLoginAPI="http://192.168.50.9:8888/xxl-job-admin/login";
        String xxlAddJobAPI="http://192.168.50.9:8888/xxl-job-admin/jobinfo/add";
        HashMap<String, Object> loginParam = new HashMap<String,Object>();
        loginParam.put("userName", "admin");
        loginParam.put("password", "123456");
        HttpResponse httpResponse = HttpRequest.post(xxlLoginAPI).form(loginParam).execute();
        if(httpResponse.getStatus() == 200) {
            System.out.println("登录成功");
            Map<String, Object> addTaskParam = new HashMap<String,Object>();
            addTaskParam.put("jobGroup", 4);// 执行器主键ID

            int year = executeTime.get(ChronoField.YEAR);
            int month = executeTime.get(ChronoField.MONTH_OF_YEAR);
            int day = executeTime.get(ChronoField.DAY_OF_MONTH);
            int hour=executeTime.get(ChronoField.HOUR_OF_DAY);
            int minute=executeTime.get(ChronoField.MINUTE_OF_HOUR);
            int second=executeTime.get(ChronoField.SECOND_OF_MINUTE);
            StringBuilder cron = new StringBuilder();
            cron.append(second);
            cron.append(" "+minute);
            cron.append(" "+hour);
            cron.append(" "+day);
            cron.append(" "+month);
            cron.append(" ?");
            cron.append(" "+year);
            System.out.println(cron.toString());
            addTaskParam.put("scheduleConf", cron.toString());// 调度配置，值含义取决于调度类型
            addTaskParam.put("jobDesc", jobDesc);//任务描述
            addTaskParam.put("author", "HuH");// 负责人
            addTaskParam.put("executorHandler", executorHandler);// 执行器，任务Handler名称
            addTaskParam.put("executorParam", JSONUtil.toJsonStr(executorParam));// 执行器，任务参数
            addTaskParam.put("scheduleType", "CRON");// 调度类型
            addTaskParam.put("executorRouteStrategy", "FIRST");
            addTaskParam.put("executorBlockStrategy", "SERIAL_EXECUTION");
            addTaskParam.put("glueType", "BEAN");
            addTaskParam.put("misfireStrategy","DO_NOTHING");
            addTaskParam.put("executorTimeout", "0");
            addTaskParam.put("executorFailRetryCount","0");
            addTaskParam.put("triggerStatus", 1);
            HttpResponse response = HttpRequest.post(xxlAddJobAPI).form(addTaskParam).execute();
            System.out.println(response.body());
            //从body中取出content content代表任务ID

            if(response.getStatus() != 200) {
                throw new Exception("任务创建失败");
            }
        }
    }
}
